<template>
  <div class="m20">
    <el-steps finish-status="success" simple :active="activeStep">
      <el-step title="第一步 汇报模板"></el-step>
      <el-step title="第二步 设置规则"></el-step>
    </el-steps>
    <FormPage :formData="formData" v-if="activeStep == 1" @nextChange="nextChange"></FormPage>
    <RulesPage
      @submitForm="submitForm"
      :reportTemplateRule="formData.reportTemplateRule"
      v-else
      ref="rulesPage"
      @backClick="backClick"
    />
  </div>
</template>
  <script>
import {
  reportTemplate,
  reportTemplateDetails,
  reportTemplateEdit
} from "@/api/reportTm";
import FormPage from "./components/formPage.vue";
import RulesPage from "./components/rulesPage.vue";
export default {
  components: { FormPage, RulesPage },
  props: [],
  data() {
    return {
      activeStep: 1,
      formData: {
        templateName: undefined,
        reportTemplateQuestionList: [],
        reportTemplateRule: {
          submitterList: [],
          recipientList: [],
          reportMode: "1",
          reportDeadlineDate: ""
        }
      }
    };
  },
  computed: {},
  watch: {},
  created() {
    console.log(this.$route.query.id);
    if (this.$route.query.id) {
      this.getInfo();
    }
  },
  mounted() {},
  methods: {
    getInfo() {
      reportTemplateDetails(this.$route.query.id).then(res => {
        if (res.code == 200) {
          console.log(res.data);
          let data = JSON.parse(JSON.stringify(res.data));
          data.reportTemplateQuestionList.map(v => {
            v.questionExtendArr = [];
            let munm = v.questionExtend.split(",");
            munm.forEach(j => {
              v.questionExtendArr.push({
                value: j
              });
            });
          });
          this.formData = data;
        }
      });
    },
    nextChange(e) {
      //   console.log(e, "dad1");
      this.formData = e;
      this.activeStep = 2;
    },
    backClick() {
      this.activeStep = 1;
    },
    submitForm(e) {
      let reportTemplateQuestionList = JSON.parse(
        JSON.stringify(this.formData.reportTemplateQuestionList)
      );
      console.log(reportTemplateQuestionList, "2");
      reportTemplateQuestionList.forEach(v => {
        let extenArr = [];
        if (v.questionExtendArr) {
          v.questionExtendArr.forEach(j => {
            extenArr.push(j.value);
          });
        }
        v.questionExtend = extenArr.join(",");
      });
      //  汇报人与汇报对象需要转成字符串
      let submitterId = [];
      let recipientId = [];
      console.log(e);
      e.submitterList.forEach(v => {
        submitterId.push(v[v.length - 1]);
      });
      e.recipientList.forEach(v => {
        recipientId.push(v[v.length - 1]);
      });
      let reportTemplateRule = JSON.parse(JSON.stringify(e));
      reportTemplateRule.submitterId = submitterId.join(",");
      reportTemplateRule.recipientId = recipientId.join(",");
      let params = {
        ...this.formData,
        reportTemplateRule,
        reportTemplateQuestionList
      };
      if (params.reportTemplateId) {
        reportTemplateEdit(params).then(res => {
          if (res.code == 200) {
            window.history.back();
          }
        });
      } else {
        reportTemplate(params).then(res => {
          if (res.code == 200) {
            window.history.back();
          }
        });
      }
    }
  }
};
</script>
  <style scoped>
.titleText {
  font-weight: bold;
}
.fs30 {
  font-size: 30px;
}
</style>
  